package com.example.schoolblog.blog.Controller;

import com.example.schoolblog.blog.Dto.PaginationDTO;
import com.example.schoolblog.blog.Model.User;
import com.example.schoolblog.blog.Service.NotificationService;
import com.example.schoolblog.blog.Service.QuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;

@Controller
public class ProfileController {

    @Autowired
    private QuestionService questionService;
    @Autowired
    private NotificationService notificationService;

    //我的问题、最新回复等列表，action代表客户端点击列表中的选项
    @GetMapping("/profile/{action}")
    public String profile(@PathVariable(name = "action") String action,
                          @RequestParam(name="page",defaultValue = "1")Integer page,
                          @RequestParam(name="size",defaultValue = "2")Integer size,
                          Model model, HttpServletRequest request){
        //获取用户
        User user = (User) request.getSession().getAttribute("user");
        //直接输入url访问则用户为空，重定向到首页
        if(user == null){
            return "redirect:/";
        }
        //根据客户端的点击封装model
        if("questions".equals(action)){
            model.addAttribute("section","questions");
            model.addAttribute("sectionName","我的提问");
            //分页获取文章列表
            PaginationDTO pagination = questionService.list(user.getId(),page,size);
            //添加到model中方便渲染页面
            model.addAttribute("pagination",pagination);
        }else if("replies".equals(action)){
            PaginationDTO paginationDTO = notificationService.list(user.getId(),page,size);
            model.addAttribute("pagination",paginationDTO);
            model.addAttribute("section","replies");
            model.addAttribute("sectionName","最新回复");
        }
        return "profile";
    }
}
